﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using BLL.Validation;
using DAL;
using Entities;
using System.Linq;
using WindowsApp.QLGiaoVien;
using DAL.IRepository;

namespace WindowsApp.QLGiaoVien
{
    public partial class frmQLGiaoVien : DevComponents.DotNetBar.Office2007Form
    {
        private IGeneralRepository<GiaoVien> _repository;
        private GiaoVienManager _manager;
        public TaiKhoan _tk;
        private bool _selectAll = false;
        public frmQLGiaoVien()
        {
            InitializeComponent();
            dgvDSGiaoVien.AutoGenerateColumns = false;
        }

        private void frmQLGiaoVien_Load(object sender, EventArgs e)
        {
            _repository = new GeneralRepository<GiaoVien>();
            _manager = new GiaoVienManager(_repository);
            dgvDSGiaoVien.DataSource = _repository.GetAll().ToList();
            XuLyPhanQuyen();            
        }

        void XuLyPhanQuyen()
        {
            if (_tk.PhanQuyen.ToUpper() == "ADMIN")
            {
                btnThem.Enabled = true;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
                mnuThemGV.Enabled = true;
                mnuSuaGV.Enabled = true;
                mnuXoaGV.Enabled = true;
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dgvDSGiaoVien.DataSource = _repository.GetAll().ToList() ;
            txtTenGV.ResetText();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new frmAddGiaoVien(_manager,this);
            frm.Text = "Thêm giáo viên";
            frm.ShowDialog();
            dgvDSGiaoVien.DataSource = _repository.GetAll().ToList();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa các giáo viên đã chọn?", "Xóa giáo viên", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            foreach (DataGridViewRow row in dgvDSGiaoVien.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
                    _manager.Delete(row.Cells[1].Value.ToString());
            }
            dgvDSGiaoVien.DataSource = _repository.GetAll().ToList();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.HoTen.IndexOf(txtTenGV.Text) >= 0);
            if (aResult.Success)
                dgvDSGiaoVien.DataSource = aResult.Result;
            else
            {
                MessageBox.Show(aResult.GetErrorMessages());
                btnLamMoi.PerformClick();
                txtTenGV.ResetText();
                txtTenGV.Focus();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvDSGiaoVien.SelectedRows.Count > 0)
            {
                GiaoVien mh = new GiaoVien();
                DataGridViewRow row = dgvDSGiaoVien.SelectedRows[0];
                mh.MaGV = row.Cells[1].Value.ToString();
                mh.HoTen = row.Cells[2].Value.ToString();
                mh.ChucVu = row.Cells[3].Value.ToString();
                mh.HocVi = row.Cells[4].Value.ToString();
                mh.DiaChi= row.Cells[5].Value.ToString();
                mh.DienThoai= row.Cells[6].Value.ToString();
                mh.Email = row.Cells[7].Value.ToString();
                
                var frm = new frmAddGiaoVien(_manager,this, mh);
                frm.Text = "Cập nhật giáo viên";
                frm.ShowDialog();
                dgvDSGiaoVien.DataSource = _repository.GetAll().ToList();
            }
            else
                MessageBox.Show("Chưa chọn giáo viên để sửa","Thông báo");

        }

        private void dgvDSGiaoVien_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSGiaoVien.Rows.Count > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < dgvDSGiaoVien.Rows.Count; idx++)
                {
                    dgvDSGiaoVien.Rows[idx].Selected = _selectAll;
                    dgvDSGiaoVien.Rows[idx].Cells[0].Value = _selectAll;
                }
            }
        }

        private void dgvDSGiaoVien_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSGiaoVien.Rows[e.RowIndex].Cells[0].Value != null)
            {
                bool selected = (bool)dgvDSGiaoVien.Rows[e.RowIndex].Cells[0].Value;
                dgvDSGiaoVien.Rows[e.RowIndex].Selected = selected;
            }
        }

        private void txtTenMH_TextChanged(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.HoTen.IndexOf(txtTenGV.Text) >= 0);
            if (aResult.Success)
                dgvDSGiaoVien.DataSource = aResult.Result;
            else
                dgvDSGiaoVien.DataSource = null;
        }
    }
}